/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author sgarreau
 */
public class DAO {

    private static final String url = "http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";

    public static List<String> getLesNums() {
        List<String> list = new ArrayList<String>();
        try {
            URL myURL = new URL(url);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());

            Element racine = doc.getDocumentElement();

            NodeList listeDep = racine.getElementsByTagName("Departement");

            for (int i = 0; i < listeDep.getLength(); i++) {
                Node Dep = listeDep.item(i);
                NodeList lesProprietes = Dep.getChildNodes();
                //recherche du nom
                for (int j = 0; j < lesProprietes.getLength(); j++) {
                    if (lesProprietes.item(j).getNodeName().equals("num")) {
                        list.add(lesProprietes.item(j).getTextContent().trim());
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;

    }

    public static String getItemByPosition(int position, List<String> lesNums) {
        return lesNums.get(position);
    }

    public static List<Medecin> getLesNoms(String Dep) {
        List<Medecin> list = new ArrayList<Medecin>();
        final String url2 = "http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/" + Dep;

        try {

            URL myURL = new URL(url2);
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(myURL.openStream());

            Element racine = doc.getDocumentElement();

            NodeList listeMed = racine.getElementsByTagName("Medecin");
            //recup des médecins
            for (int i = 0; i < listeMed.getLength(); i++) {
                Node medecin = listeMed.item(i);
                NodeList lesProprietes = medecin.getChildNodes();
                String nom = "";
                String prenom = "";
                String adresse = "";
                String specialite = "";
                String tel = "";
                //recherche du nom
                for (int j = 0; j < lesProprietes.getLength(); j++) {
                    if (lesProprietes.item(j).getNodeName().equals("nom")) {
                        nom = (lesProprietes.item(j).getTextContent().trim());
                    }
                    if (lesProprietes.item(j).getNodeName().equals("prenom")) {
                        prenom = (lesProprietes.item(j).getTextContent().trim());
                    }
                    if (lesProprietes.item(j).getNodeName().equals("adresse")) {
                        adresse = (lesProprietes.item(j).getTextContent().trim());
                    }
                    if (lesProprietes.item(j).getNodeName().equals("specialite")) {
                        specialite = (lesProprietes.item(j).getTextContent().trim());
                    }
                    if (lesProprietes.item(j).getNodeName().equals("tel")) {
                        tel = (lesProprietes.item(j).getTextContent().trim());
                    }

                }
                list.add(new Medecin(nom, prenom, adresse, specialite, tel));
            }

        } catch (Exception ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

}
